'use strict';

app.controller('setCtrl', function($rootScope, $scope, $http) {

  // console.log("set");
  //获取物品、阈值
  $scope.goods = [];
  $http.get('/api/getGoods').then(function(response){
      if(response.status==200){
          $scope.goods = response.data.goods;
      }
      else{
          console.log(response.status);
      }
  });
  //获取文件夹路径
 // $scope.rawthreshold=[];
  $http.get('api/getPath').then(function(response){
    if(response.status==200){
      var midThres=new Array(response.data.path.length);
      for(var i=0;i<response.data.path.length;i++) {
        midThres[i] = response.data.path[i].path.toString().slice(22);
        //$scope.threshold[i] = response.data.pathp[i].toString().slice(22);       
      }
      $scope.threshold = midThres;
      //console.log($scope.rawthreshold);
    }else{
      console.log(response.status);
    }
  })



  //对阈值进行减操作
	$scope.reduce = function (index) {
    
    for (var i = 0; i < $scope.goods.length; i++) {
      
      if ($scope.goods[i].id == index) {
        // console.log($scope.goods[i].threshold);
        $scope.goods[i].threshold--;
        // console.log($scope.goods[i].threshold);
        
        if ($scope.goods[i].threshold < 0 || $scope.goods[i].threshold > 100) {
          $scope.goods[i].threshold++;
          alert("阈值范围在0~100！");
        }
        break;
      }
    }
  };

  //对阈值进行加操作
	$scope.add = function () {
    //$scope.para=$scope.goods.threshold;
    for (var i = 0; i < $scope.goods.length; i++) {
      
      if ($scope.goods[i].id == index) {
        // console.log($scope.goods[i].threshold);
        $scope.goods[i].threshold += 0.1;
        // console.log($scope.goods[i].threshold);
        if ($scope.goods[i].threshold < 0 || $scope.goods[i].threshold > 1) {
          $scope.goods[i].threshold -= 0.1;
          alert("阈值范围在0~1！");
        }
        break;
      }
    }
  }

  // 更新阈值
  $scope.setSubmit = function(){
      //获取需要搜图的物品类别，保存在变量target中
      var target = {
        "wupin":"",
        "yuzhi":"",
        "thres":""
      };
      var wupin ="";
      var yuzhi ="";
      var thres ="";
      for (var i = 0; i<$scope.goods.length; i++) {
        if(document.getElementsByName("checkbox")[i].checked==true){
          wupin += $scope.goods[i].type + ",";
          yuzhi += document.getElementsByName("leibieyuzhi")[i].value + ",";
        }
      }
      for (var i=0;i<$scope.threshold.length; i++) {
        if(document.getElementsByName("wenjianjia")[i].checked==true){
          thres += $scope.threshold[i] + ",";
        }
      }
      target.wupin=wupin.slice(0,wupin.length-1);
      target.yuzhi=yuzhi.slice(0,yuzhi.length-1);
      target.thres=thres.slice(0,thres.length-1);
      $http({
        url:'/api/submit',
        method: 'POST',      
        data: target   
      }).success(function(response){
        // window.location.href = "http://localhost:3000/#/app/set";
        
      }).error(function(){
        console.log("error");
      });

  }
		
});